package com.wmlive.hhvideo.heihei.mainhome.presenter;

import android.text.TextUtils;

import com.wmlive.hhvideo.common.base.BasePresenter;
import com.wmlive.hhvideo.common.base.BaseView;
import com.wmlive.hhvideo.common.network.HttpConstant;
import com.wmlive.hhvideo.heihei.beans.discovery.MusicContentListBean;
import com.wmlive.hhvideo.heihei.beans.discovery.TopicContentListBean;
import com.wmlive.hhvideo.heihei.beans.main.AttentionResponse;
import com.wmlive.hhvideo.heihei.beans.main.ExplosionResponse;
import com.wmlive.hhvideo.heihei.beans.main.RecommendResponse;
import com.wmlive.hhvideo.heihei.beans.main.ShortVideoInfoResponse;
import com.wmlive.hhvideo.heihei.beans.main.ShortVideoItem;
import com.wmlive.hhvideo.heihei.beans.main.ShortVideoLoveResponse;
import com.wmlive.hhvideo.heihei.beans.personal.ListLikeResponse;
import com.wmlive.hhvideo.heihei.beans.personal.ListOpusResponse;
import com.wmlive.hhvideo.heihei.beans.personal.ReportType;
import com.wmlive.hhvideo.heihei.beans.personal.ReportTypeResponse;
import com.wmlive.hhvideo.heihei.beans.splash.InitCatchData;
import com.wmlive.networklib.entity.BaseResponse;
import com.wmlive.networklib.entity.DCNetObserver;

import java.util.List;

/**
 * Created by lsq on 6/19/2017.
 * RecommendNew页面的Presenter
 */

public class RecommendNewPresenter extends BasePresenter<RecommendNewPresenter.IRecommendView> {

    private int videoOffset;//视频列表的offset

    public RecommendNewPresenter(IRecommendView view) {
        super(view);
    }

    //获取推荐视频列表
    public void getRecommendVideoList(final boolean isRefresh) {
        if (!TextUtils.isEmpty(InitCatchData.opusListOpusByRecommend())) {
            executeRequest(HttpConstant.TYPE_RECOMMEND_VIDEO_LIST, getHttpApi().fetchRecommendVideo(InitCatchData.opusListOpusByRecommend(), videoOffset = isRefresh ? 0 : videoOffset))
                    .subscribe(new DCNetObserver<RecommendResponse>() {
                        @Override
                        public void onRequestDataReady(int requestCode, String message, RecommendResponse response) {
                            if (viewCallback != null) {
                                videoOffset = response.getOffset();
                                viewCallback.onVideoListOk(isRefresh, response.getRecommend_opus_list(), response.isHas_more());
                            }
                        }

                        @Override
                        public void onRequestDataError(int requestCode, int serverCode, String message) {
                            onRequestError(isRefresh ? HttpConstant.TYPE_RECOMMEND_VIDEO_LIST : (HttpConstant.TYPE_RECOMMEND_VIDEO_LIST + 1), message);
                        }
                    });
        } else {
            onRequestUrlError(HttpConstant.TYPE_RECOMMEND_VIDEO_LIST);
        }
    }

    //关注列表
    public void getFollowList(final boolean isRefresh) {
        String url = InitCatchData.opusListOpusByFollow();
        if (!TextUtils.isEmpty(url)) {
            executeRequest(HttpConstant.TYPE_FOLLOW_VIDEO_LIST, getHttpApi().fetchAttentionVideo(url, videoOffset = isRefresh ? 0 : videoOffset))
                    .subscribe(new DCNetObserver<AttentionResponse>() {
                        @Override
                        public void onRequestDataReady(int requestCode, String message, AttentionResponse response) {
                            if (viewCallback != null) {
                                videoOffset = response.getOffset();
                                viewCallback.onVideoListOk(isRefresh, response.getOpus_list(), response.isHas_more());
                            }
                        }

                        @Override
                        public void onRequestDataError(int requestCode, int serverCode, String message) {
                            onRequestError(isRefresh ? HttpConstant.TYPE_FOLLOW_VIDEO_LIST : (HttpConstant.TYPE_FOLLOW_VIDEO_LIST + 1), message);
                        }
                    });
        } else {
            onRequestUrlError(HttpConstant.TYPE_FOLLOW_VIDEO_LIST);
        }
    }

    //用户列表
    public void getUserList(final boolean isRefresh, long userId) {
        if (!TextUtils.isEmpty(InitCatchData.userListUserOpus())) {
            executeRequest(HttpConstant.TYPE_PERSONAL_PRODUCT_LIST,
                    getHttpApi().getPersonalProductList(InitCatchData.userListUserOpus(), userId, videoOffset = isRefresh ? 0 : videoOffset))
                    .subscribe(new DCNetObserver<ListOpusResponse>() {
                        @Override
                        public void onRequestDataReady(int requestCode, String message, ListOpusResponse response) {
                            if (viewCallback != null) {
                                videoOffset = response.getOffset();
                                viewCallback.onVideoListOk(isRefresh, response.getList_user_opus(), response.isHas_more());
                            }
                        }

                        @Override
                        public void onRequestDataError(int requestCode, int serverCode, String message) {
                            onRequestError(isRefresh ? HttpConstant.TYPE_PERSONAL_PRODUCT_LIST : (HttpConstant.TYPE_PERSONAL_PRODUCT_LIST + 1), message);
                        }
                    });
        } else {
            onRequestUrlError(HttpConstant.TYPE_PERSONAL_PRODUCT_LIST);
        }
    }

    //音乐列表
    public void getMusicList(final boolean isRefresh, long musicId) {
        if (!TextUtils.isEmpty(InitCatchData.musicListOpusByMusic())) {
            executeRequest(HttpConstant.TYPE_DISCOVERY_MUSIC_LIST, getHttpApi().musicOpusList(InitCatchData.musicListOpusByMusic(), musicId, videoOffset = isRefresh ? 0 : videoOffset))
                    .subscribe(new DCNetObserver<MusicContentListBean>() {
                        @Override
                        public void onRequestDataReady(int requestCode, String message, MusicContentListBean response) {
                            if (viewCallback != null) {
                                videoOffset = response.getOffset();
                                viewCallback.onVideoListOk(isRefresh, response.getMusic_opus(), response.isHas_more());
                            }
                        }

                        @Override
                        public void onRequestDataError(int requestCode, int serverCode, String message) {
                            onRequestError(isRefresh ? HttpConstant.TYPE_DISCOVERY_MUSIC_LIST : (HttpConstant.TYPE_DISCOVERY_MUSIC_LIST + 1), message);
                        }
                    });
        } else {
            onRequestUrlError(HttpConstant.TYPE_DISCOVERY_MUSIC_LIST);
        }
    }

    //话题列表
    public void getTopicList(final boolean isRefresh, long topicId) {
        if (!TextUtils.isEmpty(InitCatchData.topicListOpusByTopic())) {
            executeRequest(HttpConstant.TYPE_DISCOVERY_TOPIC_LIST, getHttpApi().topicOpusList(InitCatchData.topicListOpusByTopic(), topicId, videoOffset = isRefresh ? 0 : videoOffset))
                    .subscribe(new DCNetObserver<TopicContentListBean>() {
                        @Override
                        public void onRequestDataReady(int requestCode, String message, TopicContentListBean response) {
                            if (viewCallback != null) {
                                videoOffset = response.getOffset();
                                viewCallback.onVideoListOk(isRefresh, response.getTopic_opus(), response.isHas_more());
                            }
                        }

                        @Override
                        public void onRequestDataError(int requestCode, int serverCode, String message) {
                            onRequestError(isRefresh ? HttpConstant.TYPE_DISCOVERY_TOPIC_LIST : (HttpConstant.TYPE_DISCOVERY_TOPIC_LIST + 1), message);
                        }
                    });
        } else {
            onRequestUrlError(HttpConstant.TYPE_DISCOVERY_TOPIC_LIST);
        }
    }

    //爆点列表
    public void getExplosionList(final boolean isRefresh) {
        if (!TextUtils.isEmpty(InitCatchData.opusListOpusByTime())) {
            executeRequest(HttpConstant.TYPE_EXPLOSION_VIDEO, getHttpApi().explosionVideo(InitCatchData.opusListOpusByTime(), videoOffset = isRefresh ? 0 : videoOffset))
                    .subscribe(new DCNetObserver<ExplosionResponse>() {
                        @Override
                        public void onRequestDataReady(int requestCode, String message, ExplosionResponse response) {
                            if (viewCallback != null) {
                                videoOffset = response.getOffset();
                                viewCallback.onVideoListOk(isRefresh, response.getTimeOpusList(), response.isHas_more());
                            }
                        }

                        @Override
                        public void onRequestDataError(int requestCode, int serverCode, String message) {
                            onRequestError(isRefresh ? HttpConstant.TYPE_EXPLOSION_VIDEO : (HttpConstant.TYPE_EXPLOSION_VIDEO + 1), message);
                        }
                    });
        } else {
            onRequestUrlError(HttpConstant.TYPE_EXPLOSION_VIDEO);
        }
    }

    //喜欢列表
    public void getPersonaLovelList(final boolean isRefresh, long userId) {
        String url = InitCatchData.userListUserLike();
        if (!TextUtils.isEmpty(url)) {
            executeRequest(HttpConstant.TYPE_PERSONAL_LIKE_LIST, getHttpApi().getPersonalLoveList(url, userId, videoOffset = isRefresh ? 0 : videoOffset))
                    .subscribe(new DCNetObserver<ListLikeResponse>() {
                        @Override
                        public void onRequestDataReady(int requestCode, String message, ListLikeResponse response) {
                            if (null != viewCallback) {
                                videoOffset = response.getOffset();
                                viewCallback.onVideoListOk(isRefresh, response.getUser_like_list(), response.isHas_more());
                            }
                        }

                        @Override
                        public void onRequestDataError(int requestCode, int serverCode, String message) {
                            onRequestError(isRefresh ? HttpConstant.TYPE_PERSONAL_LIKE_LIST : (HttpConstant.TYPE_PERSONAL_LIKE_LIST + 1), message);
                        }
                    });
        } else {
            onRequestUrlError(HttpConstant.TYPE_PERSONAL_LIKE_LIST);
        }
    }

    //获取视频的详情
    public void getVideoDetail(final int position, final long id) {
        if (!TextUtils.isEmpty(InitCatchData.opusGetOpus())) {
            executeRequest(HttpConstant.TYPE_VIDEO_INFO, getHttpApi().fetchVideoInfo(InitCatchData.opusGetOpus(), id))
                    .subscribe(new DCNetObserver<ShortVideoInfoResponse>() {
                        @Override
                        public void onRequestDataReady(int requestCode, String message, ShortVideoInfoResponse response) {
                            if (null != viewCallback) {
                                viewCallback.onVideoDetailOk(id, position, response);
                            }
                        }

                        @Override
                        public void onRequestDataError(int requestCode, int serverCode, String message) {
                            if (serverCode == 30005) {//视频被删除了
                                if (null != viewCallback) {
                                    viewCallback.onVideoError(position, id, message);
                                }
                            } else {
                                onRequestError(HttpConstant.TYPE_VIDEO_INFO, message);
                            }
                        }
                    });
        } else {
            onRequestUrlError(HttpConstant.TYPE_VIDEO_INFO);
        }
    }

    //点赞
    public void likeVideo(final int position, final long videoId) {
        String url = InitCatchData.opusLikeOpus();
        if (!TextUtils.isEmpty(url)) {
            executeRequest(HttpConstant.TYPE_VIDEO_LOVE, getHttpApi().loveVideo(url, videoId))
                    .subscribe(new DCNetObserver<ShortVideoLoveResponse>() {
                        @Override
                        public void onRequestDataReady(int requestCode, String message, ShortVideoLoveResponse response) {
                            if (null != viewCallback) {
                                viewCallback.onLikeOk(videoId, position, response);
                            }
                        }

                        @Override
                        public void onRequestDataError(int requestCode, int serverCode, String message) {
                            if (serverCode == 30005) {//视频被删除了
                                if (null != viewCallback) {
                                    viewCallback.onVideoError(position, videoId, message);
                                }
                            } else {
                                onRequestError(HttpConstant.TYPE_VIDEO_LOVE, message);
                            }
                        }
                    });

        } else {
            onRequestUrlError(HttpConstant.TYPE_VIDEO_LOVE);
        }
    }

    //关注作者
    public void follow(final int position, final long videoId, long userId) {
        String url = InitCatchData.userFollowUser();
        if (!TextUtils.isEmpty(url)) {
            executeRequest(HttpConstant.TYPE_USER_FOLLOW, getHttpApi().followAnchor(url, userId))
                    .subscribe(new DCNetObserver<BaseResponse>() {
                        @Override
                        public void onRequestDataReady(int requestCode, String message, BaseResponse response) {
                            if (null != viewCallback) {
                                viewCallback.onFollowOk(position, videoId);
                            }
                        }

                        @Override
                        public void onRequestDataError(int requestCode, int serverCode, String message) {
                            onRequestError(HttpConstant.TYPE_USER_FOLLOW, message);
                        }
                    });
        } else {
            onRequestUrlError(HttpConstant.TYPE_USER_FOLLOW);
        }
    }

    //获取举报列表
    public void getReportType() {
        String url = InitCatchData.opusListReportType();
        if (!TextUtils.isEmpty(url)) {
            executeRequest(HttpConstant.TYPE_REPORT_TYPE_LIST, getHttpApi().fetchReportType(url))
                    .subscribe(new DCNetObserver<ReportTypeResponse>() {
                        @Override
                        public void onRequestDataReady(int requestCode, String message, ReportTypeResponse response) {
                            if (viewCallback != null) {
                                viewCallback.onReportListOk(response.getReport_type_list());
                            }
                        }

                        @Override
                        public void onRequestDataError(int requestCode, int serverCode, String message) {
                            onRequestError(HttpConstant.TYPE_REPORT_TYPE_LIST, message);
                        }
                    });
        } else {
            onRequestUrlError(HttpConstant.TYPE_REPORT_TYPE_LIST);
        }
    }

    //举报作品
    public void reportWorks(final int position, final long videoId, int reportId) {
        String url = InitCatchData.opusReport();
        if (!TextUtils.isEmpty(url)) {
            executeRequest(HttpConstant.TYPE_REPORT,
                    getHttpApi().reportWorks(url, videoId, reportId))
                    .subscribe(new DCNetObserver<BaseResponse>() {
                        @Override
                        public void onRequestDataReady(int requestCode, String message, BaseResponse response) {
                            if (viewCallback != null) {
                                viewCallback.onReportOk();
                            }
                        }

                        @Override
                        public void onRequestDataError(int requestCode, int serverCode, String message) {
                            if (serverCode == 30005) {//视频被删除了
                                if (null != viewCallback) {
                                    viewCallback.onVideoError(position, videoId, message);
                                }
                            } else {
                                onRequestError(HttpConstant.TYPE_REPORT, message);
                            }
                        }
                    });

        } else {
            onRequestUrlError(HttpConstant.TYPE_REPORT);
        }
    }

    //删除视频
    public void deleteVideo(final int position, final long shortVideoId) {
        String url = InitCatchData.opusDeleteOpus();
        if (!TextUtils.isEmpty(url)) {
            executeRequest(HttpConstant.TYPE_VIDEO_DELETE
                    , getHttpApi().deleteVideo(url, shortVideoId))
                    .subscribe(new DCNetObserver<BaseResponse>() {
                        @Override
                        public void onRequestDataReady(int requestCode, String message, BaseResponse response) {
                            if (viewCallback != null) {
                                viewCallback.onDeleteVideoOk(position, shortVideoId);
                            }
                        }

                        @Override
                        public void onRequestDataError(int requestCode, int serverCode, String message) {
                            onRequestError(HttpConstant.TYPE_VIDEO_DELETE, message);
                        }
                    });
        } else {
            onRequestUrlError(HttpConstant.TYPE_VIDEO_DELETE);
        }
    }

    public void setVideoOffset(int videoOffset) {
        this.videoOffset = videoOffset;
    }

    public interface IRecommendView extends BaseView {
        void onVideoListOk(boolean isRefresh, List<ShortVideoItem> list, boolean hasMore);

        void onVideoDetailOk(long videoId, int position, ShortVideoInfoResponse videoInfoBean);

        void onVideoError(int position, long videoId, String message);

        void onReportListOk(List<ReportType> list);

        void onLikeOk(long videoId, int position, ShortVideoLoveResponse bean);

        void onReportOk();

        void onFollowOk(final int position, final long videoId);

        void onDeleteVideoOk(int position, long videoId);
    }
}
